<?php
namespace app\admin\logic;

use app\admin\model\Gcategory;

class Goodslogic
{

    // 搜索器条件
    public static function setSearchSdata($param)
    {
        $sdata = [];
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $sdata['keywords'] = $param['keywords'];
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            $sdata['if_show'] = $param['if_show'];
        }
        if (isset($param['recommended']) && $param['recommended']<>''){
            $sdata['recommended'] = $param['recommended'];
        }
        if (isset($param['mintime']) && !empty($param['mintime'])){
            $sdata['mintime'] = $param['mintime'];
        }
        if (isset($param['maxtime']) && !empty($param['maxtime'])){
            $sdata['maxtime'] = $param['maxtime'];
        }
        if (isset($param['minmoney']) && !empty($param['minmoney'])){
            $sdata['minmoney'] = $param['minmoney'];
        }
        if (isset($param['maxmoney']) && !empty($param['maxmoney'])){
            $sdata['maxmoney'] = $param['maxmoney'];
        }
        if (isset($param['b_brand']) && !empty($param['b_brand'])){
            $sdata['b_brand'] = $param['b_brand'];
        }
        if (isset($param['pid']) && !empty($param['pid'])){
            $sdata['pid'] = $param['pid'];
        }

        return $sdata;
    }
    // 留值
    public static function setData($param)
    {

        $data = [
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
            'mintime'=>isset($param['mintime'])?$param['mintime']:'',
            'maxtime'=>isset($param['maxtime'])?$param['maxtime']:'',
            'minmoney'=>isset($param['minmoney'])?$param['minmoney']:'',
            'maxmoney'=>isset($param['maxmoney'])?$param['maxmoney']:'',
            'b_brand'=>isset($param['b_brand'])?$param['b_brand']:'',
            'msg'=>'',
            'pid'=>isset($param['pid'])?$param['pid']:'',
        ];
        return $data;

    }
    // 时间排序
    public static function getTimeOrder($cid)
    {
        $msg='';
        $order=['goods_id'=>'asc'];
        if ($cid==1){
            $order=['add_time'=>'desc'];
            $msg='↓';
        }
        if ($cid==2){
            $order=['add_time'=>'asc'];
            $msg='↑';
        }
        if ($cid==3){
            $order=['goods_id'=>'asc'];
            $cid=0;
        }
        return [$order,$cid,$msg];
    }
    // 分类递归
    public static function getCate()
    {
        $cate = Gcategory::getCateSel([]);
        $arr = [];
        foreach ($cate as $v){
            if ($v['parent_id']==0){
                $arr[$v['cate_id']] = $v;
                $arr[$v['cate_id']]['next'] = self::getNextCate($cate,$v['cate_id']);
            }
        }
        return $arr;
    }
    public static function getNextCate($cate,$parent_id)
    {
        $arr = [];
        foreach ($cate as $vv) {
            if ($parent_id == $vv['parent_id']) {
                $arr[$vv['cate_id']] = $vv;
                $arr[$vv['cate_id']]['next'] = self::getNextCate($cate,$vv['cate_id']);
            }
        }
        return $arr;
    }

}
